package com.heima.utils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.utils.mapper.ApArticleContentMapper;
import com.heima.utils.mapper.ApArticleMapper;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ImportEsData {
    @Autowired
    RestHighLevelClient client;

    @Autowired
    ApArticleMapper apArticleMapper;

    @Autowired
    ApArticleContentMapper apArticleContentMapper;


    @Test
    public void importEsDataTest() throws IOException {
        String indexName = "app_info_article";
        boolean b = isIndexExist(indexName);
        if (b)
            deleteIndex(indexName);
        createIndex(indexName,"{\n" +
                "      \"properties\" : {\n" +
                "        \"authorId\" : {\n" +
                "          \"type\" : \"integer\"\n" +
                "        },\n" +
                "        \"content\" : {\n" +
                "          \"type\" : \"text\",\n" +
                "          \"analyzer\" : \"ik_smart\"\n" +
                "        },\n" +
                "        \"id\" : {\n" +
                "          \"type\" : \"keyword\"\n" +
                "        },\n" +
                "        \"images\" : {\n" +
                "          \"type\" : \"keyword\"\n" +
                "        },\n" +
                "        \"layout\" : {\n" +
                "          \"type\" : \"short\"\n" +
                "        },\n" +
                "        \"publishTime\" : {\n" +
                "          \"type\" : \"date\"\n" +
                "        },\n" +
                "        \"title\" : {\n" +
                "          \"type\" : \"text\",\n" +
                "          \"analyzer\" : \"ik_smart\"\n" +
                "        }\n" +
                "      }\n" +
                "    }");
        List<ApArticle> list = apArticleMapper.loadArticleList();
        BulkRequest request = new BulkRequest(indexName);
        for (ApArticle apArticle : list) {
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));

            Map<String,Object> map = new HashMap<>();
            map.put("id", String.valueOf(apArticle.getId()));
            map.put("publishTime", apArticle.getPublishTime());
            map.put("layout", apArticle.getLayout());
            map.put("images", apArticle.getImages());
            map.put("authorId", apArticle.getAuthorId());
            map.put("title", apArticle.getTitle());
            map.put("content",apArticleContent.getContent());
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(String.valueOf(apArticle.getId()));
            indexRequest.source(JSON.toJSONString(map),XContentType.JSON);
            request.add(indexRequest);
        }
        client.bulk(request,RequestOptions.DEFAULT);

    }

    private void createIndex(String indexName, String mappings) throws IOException {
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        request.mapping(mappings, XContentType.JSON);
        client.indices().create(request,RequestOptions.DEFAULT);
    }

    private void deleteIndex(String indexName) throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest(indexName);
        client.indices().delete(request,RequestOptions.DEFAULT);
    }

    private boolean isIndexExist(String indexName) throws IOException {
        GetIndexRequest request = new GetIndexRequest(indexName);
        return client.indices().exists(request, RequestOptions.DEFAULT);
    }
}
